Conversation
|
Went through this in detail — no issues found. Specifically checked:
Good fix, nice test coverage (including the zstandard third-party-codec case, which has a different close-ownership shape than the stdlib gzip/bz2/lzma wrappers). |
| compressed = compress(f, mode=mode[0]) | ||
| if compressed is not f: | ||
| f = _ClosingFile(compressed, f) |
There was a problem hiding this comment.
Wouldn't it make sense to put the call to _ClosingFile inside compress() ?
|
Worth flagging one snag with moving it into Putting it in the registry lookup instead would reach the other call sites that read Keeping the wrapping at the |
|
You make a good point, but then we should also consider other filesystem-specific open() implementations that accept compression= ? Maybe there are none (because _open is usually what is used). |
|
You're right that there are none — So the base-class call site covers everything today, and I'd keep it there. One improvement worth making: give |
|
Yes, I like moving it to compression.py |
|
😃😃 |
|
(ping me when you've done that. I can handle the conflict if you like - it's just the changelog) |
|
shall i go for it? happy to do it myself if @Jokasa7 doesnt, nd if u need it quick |
|
no rush |
|
Sounds good — I'll leave it with @Jokasa7 then, it's their patch. @Jokasa7 if you'd rather not pick up the refactor, just say and I'll open a follow-up. Otherwise it's a small move: lift the compress(...) call and the _ClosingFile wrap into a helper in compression.py and have AbstractFileSystem.open call that — the other compr[...] callers stay as they are. |
Closing a file returned by
AbstractFileSystem.open(..., compression=...)can leave the underlying filesystem file open. For codecs such as gzip, bz2 and lzma, closing the compression stream does not close a caller-provided file. This delays resource release and can defer a write/upload error until garbage collection.Keep ownership of both streams in a private wrapper. Close the compression stream first to finish its trailer, then close the underlying file in
finally. Preserve the unwrapped object when compression inference selects no codec, and keep transaction queues pointing to the raw file.The returned object retains the file-like interface but is a wrapper, so its concrete type is no longer the codec class. This avoids replacing methods on codec objects, including C extension streams that do not allow setting
close.Fixes #1672.
Validation on Python 3.12.13 / Windows:
master: 24 leave the raw file open and six fail to propagate its close error.test_glob_weird_characterscases that try to create a directory containing|, which Windows rejects withWinError 123; the same failures were reproduced on unmodifiedmaster.git diff --check, and a full Sphinx HTML build with-W --keep-goingpass. The optional full cloud/backend and downstream integration suites were not run locally.AI assistance: this patch and its regression tests were developed and validated with OpenAI Codex, including a separate code review. The test results above are from commands executed by Codex.